<?php
##############################################
#
#   订单管理
# @ Time : 2017-1-3 - 下午10:45:24
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################

namespace Admin\Controller;
use Admin\Model\AdminBaseModel; 
use Admin\Model\OrderModel;


class OrderController extends AdminBaseController {
	
	protected $_order;
	
	public function _initialize(){
		parent::_initialize();
		$this->_order = new OrderModel();
	}
	
	/**
	 * 订单列表
	 */
	public function orderList(){
	    $add_time = I('add_time');
	    if($add_time){
	        $gap = explode(' - ', $add_time);
	        $start_time = strtotime($gap[0]);
	        $end_time = strtotime($gap[1]);
	    }
	    $data = $this->_order->getOrder(I('activity_name'), I('order_sn'), I('order_status'), I('payment_stautus'), $start_time, $end_time);
	    $this -> assign('order', $data[0]);
	    $this -> assign('page', $data[1]);
	    $this -> display('index');
	}
	
	/**
	 * 添加活动
	 */
 	public function addOrder(){
		if(IS_GET){
		    $id = I('id');
		    $act = I('get.act');
		    if($id && $act == 'edit'){
		        $data = $this->_order->getOrderFind($id);
		        $this->assign('id', $id);
		        $this->assign('order', $data);
		    }
		    $this->assign('act', $act);
		    $this->display('add');
		}else if(IS_POST){
		    $data = array('name'=>I('name'), 'mobile'=>I('mobile'), 'number'=>I('number'),
		                  'order_status'=>I('order_status'), 'payment_status'=>I('payment_status'));
		   	$admin_base_model = new AdminBaseModel('activity_order');
		   	$act = I('post.act');
		   	if($act == 'add'){
		   	    $result = $admin_base_model->addData($data);
		   	    if($result)
		   	        $this->success('添加成功', U('orderList'), 1);
		   	    else
		   	        $this->error('操作失败', '', 1);
		   	}elseif($act == 'edit'){
		   	    $id = I('id');
		   	    if($this->_order->editOrder($id, $data) !== false)
		   	        $this -> success('修改成功', U('orderList'), 1);
		   	    else
		   	        $this -> error('操作失败', '', 1);
		   	}
    	}
	}

	/**
	 * 全选删除订单
	 */
    public function deleteAllOrder(){
	    if(!IS_POST) return;
	    $result = $this->_order->deleteAllOrder($_POST);
	    if($result)
			$this -> success('删除成功', U('orderList'), 2);
		else
			$this -> error('操作失败', '', 2);
	}
	
	/**
	 * 删除订单
	 */
	public function deleteOrder(){
		if(!IS_AJAX) return;
		$result = $this->_order->deleteOrder(intval($_GET['id']));
		if($result)
			$this->ajaxReturn(array('status' => 1,'message' => '删除成功'));
		else
			$this->ajaxReturn(array('status' => 2,'message' => '操作失败'));
	}
	
	/**
	 * 操作
	 */
	public function operate(){
	    $operate = I('post.operate');
	    $selected_id_arr = I('post.order_id');
	    if(!in_array($operate,array('del','order_status_1','order_status_2','order_status_3','order_status_4','payment_status_1','payment_status_2')) || !is_array($selected_id_arr)){
	        $this->error('非法操作');
	    }
	    $map['id'] = array('in', $selected_id_arr);
	    $activity_order = M('activity_order');
	    switch ($operate){
	        case 'del':
	            $row = $activity_order->where($map)->delete();
	            break;
	        case 'order_status_1':
	            $data = array('order_status'=>1);
	            $row = $activity_order->where($map)->save($data);
	            break;
	        case 'order_status_2':
	            $data = array('order_status'=>2);
	            $row = $activity_order->where($map)->save($data);
	            break;
	        case 'order_status_3':
	            $data = array('order_status'=>3);
	            $row = $activity_order->where($map)->save($data);
	            break;
	        case 'order_status_4':
	            $data = array('order_status'=>4);
	            $row = $activity_order->where($map)->save($data);
	            break;
	        case 'payment_status_1':
	            $data = array('payment_status'=>1);
	            $row = $activity_order->where($map)->save($data);
	            break;
	        case 'payment_status_2':
	            $data = array('payment_status'=>2);
	            $row = $activity_order->where($map)->save($data);
	            break;
	    }
	    if($row !== false)
	       $this->success('操作成功', U('orderList'), 1);
	    else
	        $this->error('操作失败');
	
	}
	
	
}



	
	